﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LosScrum.Models;

namespace LosScrum.Controllers
{
    public class ReservaController : Controller
    {
        //Pedro Chambi Ochoa
        private List<Reserva> CrearReservas()
        {
            List<Reserva> reservas = new List<Reserva>();

            reservas.Add(new Reserva() { Codigo = 1, Recurso = 1, Alumno = 1, fechaInicio = DateTime.Parse("01/10/2012"), fechaFin = DateTime.Parse("01/10/2012") });
            
            return reservas;
        }

        private Reserva ObtenerReservas(int codigo)
        {  
            List<Reserva> reservas = (List<Reserva>)Session["reservas"];

            Reserva model = reservas.Single(delegate(Reserva reserva)
            {
                if (reserva.Codigo == codigo) return true;
                else return false;
            });
            return model;



        }

        //
        // GET: /Recursos/

        public ActionResult Index()
        {
            if (Session["reservas"] == null)
                Session["reservas"] = CrearReservas();
            List<Reserva> model = (List<Reserva>)Session["reservas"];
            return View(model);
        }

        //
        // GET: /Recursos/Details/5

        public ActionResult Details(int id)
        {
            Reserva model = ObtenerReservas(id);
            return View(model);
        }

        //
        // GET: /Recursos/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Recursos/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                List<Reserva> reservas = (List<Reserva>)Session["reservas"];
                reservas.Add(new Reserva()
                {
                    Codigo = int.Parse(collection["Codigo"]),
                    Recurso = int.Parse(collection["Recurso"]),
                    Alumno = int.Parse(collection["Alumno"]),
                    fechaInicio = DateTime.Parse(collection["fechaInicio"]),
                    fechaFin = DateTime.Parse(collection["fechaFin"]),
                });

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /Recursos/Edit/5
 
        public ActionResult Edit(int id)
        {
            Reserva model = ObtenerReservas(id);
            return View(model);
        }

        //
        // POST: /Recursos/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                Reserva model = ObtenerReservas(id);
                model.Alumno= int.Parse(collection["Alumno"]);
                model.Recurso = int.Parse(collection["Estado"]);
                model.fechaInicio= DateTime.Parse(collection["fechaInicio"]);
                model.fechaFin = DateTime.Parse(collection["fechaFin"]);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Recursos/Delete/5
 
        public ActionResult Delete(int id)
        {
            Reserva model = ObtenerReservas(id);
            return View(model);
        }

        //
        // POST: /Recursos/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                List<Reserva> reservas = (List<Reserva>)Session["reservas"];
                reservas.Remove(ObtenerReservas(id));
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
